package service

import (
	"server/global"
	"server/plugin/mall/model"
	"server/plugin/mall/model/request"
	"gorm.io/gorm"
)

var A商品1 = new(shangpin1)

type shangpin1 struct{}

// CreateA商品1 创建商品1记录
// Author [yourname](https://github.com/yourname)
func (s *shangpin1) CreateA商品1(shangpin1 *model.A商品1) (err error) {
	err = global.GVA_DB.Create(shangpin1).Error
	return err
}

// DeleteA商品1 删除商品1记录
// Author [yourname](https://github.com/yourname)
func (s *shangpin1) DeleteA商品1(ID string, userID uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&model.A商品1{}).Where("id = ?", ID).Update("deleted_by", userID).Error; err != nil {
			return err
		}
		if err = tx.Delete(&model.A商品1{}, "id = ?", ID).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// DeleteA商品1ByIds 批量删除商品1记录
// Author [yourname](https://github.com/yourname)
func (s *shangpin1) DeleteA商品1ByIds(IDs []string, deleted_by uint) (err error) {
	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		if err := tx.Model(&model.A商品1{}).Where("id in ?", IDs).Update("deleted_by", deleted_by).Error; err != nil {
			return err
		}
		if err := tx.Where("id in ?", IDs).Delete(&model.A商品1{}).Error; err != nil {
			return err
		}
		return nil
	})
	return err
}

// UpdateA商品1 更新商品1记录
// Author [yourname](https://github.com/yourname)
func (s *shangpin1) UpdateA商品1(shangpin1 model.A商品1) (err error) {
	err = global.GVA_DB.Model(&model.A商品1{}).Where("id = ?", shangpin1.ID).Updates(&shangpin1).Error
	return err
}

// GetA商品1 根据ID获取商品1记录
// Author [yourname](https://github.com/yourname)
func (s *shangpin1) GetA商品1(ID string) (shangpin1 model.A商品1, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&shangpin1).Error
	return
}

// GetA商品1InfoList 分页获取商品1记录
// Author [yourname](https://github.com/yourname)
func (s *shangpin1) GetA商品1InfoList(info request.A商品1Search) (list []model.A商品1, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.A商品1{})
	var shangpin1s []model.A商品1
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.A名称 != nil {
		db = db.Where("名称 LIKE ?", "%"+*info.A名称+"%")
	}
	if info.A类别 != nil {
		db = db.Where("类别 = ?", *info.A类别)
	}
	// if info.A关键词 != nil {
	//     db = db.Where("关键词 = ?",*info.A关键词)
	// }
	if info.A价格 != nil {
		db = db.Where("价格 <> ?", *info.A价格)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	var OrderStr string
	orderMap := make(map[string]bool)
	orderMap["名称"] = true
	orderMap["类别"] = true
	if orderMap[info.Sort] {
		OrderStr = info.Sort
		if info.Order == "descending" {
			OrderStr = OrderStr + " desc"
		}
		db = db.Order(OrderStr)
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}
	err = db.Find(&shangpin1s).Error
	return shangpin1s, total, err
}
func (s *shangpin1) GetA商品1DataSource() (res map[string][]map[string]any, err error) {
	res = make(map[string][]map[string]any)

	类别 := make([]map[string]any, 0)

	global.GVA_DB.Table("类别设置").Where("deleted_at IS NULL").Select("类别 as label,id as value").Scan(&类别)
	res["类别"] = 类别
	return
}

func (s *shangpin1) GetA商品1Public() {

}
